﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Drawing;
using System.Reflection;
using Epam.PhotoGallery.Interfaces.DataTransferObjects;
using System.IO;

namespace Epam.PhotoGallery.Site.CustomModelBinders
{
    public class CategoryModelBinder : DefaultModelBinder
    {
        private const string IMAGE_PROPERTY_NAME = "Images";
        private const string IMAGE_URL_PROPERTY_NAME = "ImagesUrl";

        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var resultObj = base.BindModel(controllerContext, bindingContext);

            ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(string.Concat(bindingContext.ModelName, ".", IMAGE_PROPERTY_NAME));

            if (valueResult == null)
            {
                valueResult = bindingContext.ValueProvider.GetValue(IMAGE_PROPERTY_NAME);
               
            }

            if (ResultIsNotNullOrEmpty(valueResult))
            {
                FillImage(resultObj, valueResult, controllerContext, bindingContext);
            }
            else
            {
                valueResult = bindingContext.ValueProvider.GetValue(IMAGE_URL_PROPERTY_NAME);

                if (valueResult != null)
                {
                    FillUrlImage(resultObj, valueResult, controllerContext, bindingContext);
                }
            }

            return resultObj;
        }

        private void FillImage(object resultObj, ValueProviderResult valueResult, ControllerContext controllerContext, ModelBindingContext bindingContext)
        {

            HttpPostedFileBase file = valueResult.ConvertTo(typeof(HttpPostedFileBase)) as HttpPostedFileBase;

            if (file != null)
            {
                CommunityImage image = new CommunityImage(ImageStorageType.Database);
                byte[] imageContent = new byte[file.ContentLength];

                file.InputStream.Read(imageContent, 0, file.ContentLength - 1);
                image.ImageData.Content = imageContent;
                image.ImageData.MimeType = file.ContentType;

                PropertyInfo imageProperty = bindingContext.ModelType.GetProperty(IMAGE_PROPERTY_NAME);

                imageProperty.SetValue(resultObj, image, null);
            }
        }

        private void FillUrlImage(object resultObj, ValueProviderResult valueResult, ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            string url = valueResult.ConvertTo(typeof(string)).ToString();

            CommunityImage image = new CommunityImage(ImageStorageType.Url);

            image.Path = url;

            PropertyInfo imageProperty = bindingContext.ModelType.GetProperty(IMAGE_PROPERTY_NAME);

            imageProperty.SetValue(resultObj, image, null);

        }

        private bool ResultIsNotNullOrEmpty(ValueProviderResult valueResult)
        {
            return valueResult != null && ((HttpPostedFileBase)valueResult.ConvertTo(typeof(HttpPostedFileBase)))!=null;
        }
    }
}